package com.im.app.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.im.app.model.Conversations;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface ConversationsMapper extends BaseMapper<Conversations> {

    @Select(value = "select * from conversations where owner_user_id = #{ownerUserId} and group_id = #{groupId} limit 1")
    Conversations selectByGroup(@Param("ownerUserId") String ownerUserId, @Param("groupId") String groupId);

    @Select(value = "select * from conversations where owner_user_id = #{ownerUserId} and user_id = #{userId} limit 1")
    Conversations selectByUser(@Param("ownerUserId") String ownerUserId,@Param("userId") String userId);

    @Select(value = "select * from conversations where conversation_id = #{conversationId}")
    Conversations selectById(@Param("conversationId") String conversationId);

    @Update(value = "update conversations set is_pinned = #{isPinned} where conversation_id=#{conversationId}")
    int updateIsPinned(@Param("isPinned") Integer isPinned,@Param("conversationId") String conversationId);

    @Update(value = "update conversations set is_black = #{isBlack} where owner_user_id=#{ownerUserId} and user_id = #{userId}")
    int updateIsBlack(@Param("isBlack") Integer isBlack,@Param("ownerUserId") String ownerUserId,@Param("userId") String userId);

    @Update(value = "update conversations set nick_name = #{nickName} where conversation_id=#{conversationId}")
    int updateNick(@Param("nickName") String nickName,@Param("conversationId") String conversationId);

    @Update(value = "update conversations set unread_count = 0 where conversation_id=#{conversationId}")
    int clearUnreadCount(@Param("conversationId") String conversationId);

    @Select(value = "select * from conversations where is_deleted = 0 and owner_user_id = #{ownerUserId}  order by is_pinned desc,last_time desc")
    List<Conversations> getConversationsList(@Param("ownerUserId") String ownerUserId);
    @Select(value = "select * from conversations where is_deleted = 0 and owner_user_id = #{ownerUserId} and nick_name like CONCAT('%', #{keyWord},'%') order by is_pinned desc,last_time desc")
    List<Conversations> getConversationByKeyWord(@Param("ownerUserId") String ownerUserId,@Param("keyWord") String keyWord);

    @Update(value = "update conversations set is_deleted = 1 where conversation_id=#{conversationId}")
    int clearConversations(@Param("conversationId") String conversationId);

    @Select(value = "select * from conversations where conversation_id=#{conversationId}")
    Conversations selectLastMessage(@Param("conversationId") String conversationId);

    @Select(value = "select * from conversations where group_id = #{groupId}")
    List<Conversations> selectByGroupId( @Param("groupId") String groupId);

    @Delete(value = "delete from conversations where (owner_user_id=#{userId} or user_id = #{userId}) and create_time < date_sub(SYSDATE(),interval #{days} day) ")
    int clearByDays(@Param("userId") String userId,@Param("days") Integer days);

    @Delete(value = "delete from conversations where create_time < date_sub(SYSDATE(),interval #{days} day) ")
    int clearByDaysOut(@Param("days") Integer days);

    @Select(value = "select * from conversations where (owner_user_id=#{userId} or user_id = #{userId}) and create_time < date_sub(SYSDATE(),interval #{days} day)")
    List<Conversations> selectByDays(@Param("userId") String userId,@Param("days") Integer days);

    @Select(value = "select * from conversations where create_time < date_sub(SYSDATE(),interval #{days} day)")
    List<Conversations> selectByDaysOut(@Param("days") Integer days);

    int updateCon(String userId,String nickName,String avatar);
}

